/**
 * 
 */
package Distributive;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import cardCollections.Hand;

/**
 * @author CAT
 * 
 */
public class blackJackWatcher {

	private Socket playerSocket;
	private ObjectInputStream cardInput;
	public static ArrayList<Hand> listOfDealtHands;
	private Object testHand;
	private FileWriter cardOutPut;
	private BufferedWriter buffer;

	/**
	 * @param args
	 * @throws IOException
	 * @throws UnknownHostException
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws UnknownHostException,
			IOException, ClassNotFoundException {
		blackJackWatcher theConsumer = new blackJackWatcher();
		System.out.println("Client started");
		theConsumer.executeClient();
		System.out.println();
		System.out.println("Client quitting");
	}

	/**
	 * Initialized the cardOutPut to a new FileWriter() Initialized the
	 * playerScoket to a new Socket() Initialized the ObjectInputStream()
	 * 
	 * In the try: Initializes testHand to the object read in by cardInput.
	 * Appends the testHand to the text file, through the buffer.
	 * 
	 * @throws UnknownHostException
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	private void executeClient() throws UnknownHostException, IOException,
			ClassNotFoundException {
		cardOutPut = new FileWriter("cardDealt.txt", true);
		buffer = new BufferedWriter(cardOutPut);
		this.playerSocket = new Socket("127.0.0.1", 8189);
		this.cardInput = new ObjectInputStream(this.playerSocket
				.getInputStream());

		try {
			testHand = this.cardInput.readObject();
			buffer.append(testHand.toString());
			System.out.println(testHand);
			buffer.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (java.net.SocketException e) {
			System.out.println();
			System.out.println("Client quitting");
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.cardInput.close();
		this.cardOutPut.close();
		this.playerSocket.close();
	}
}
